package com.hailin.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hailin.lease.model.entity.*;
import com.hailin.lease.web.admin.mapper.LeaseAgreementMapper;
import com.hailin.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hailin.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.hailin.lease.web.admin.vo.agreement.AgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;

    /**
     * 分页查询
     * @param current
     * @param size
     * @param queryVo
     * @return
     */
    public IPage<AgreementVo> pageQuery(long current, long size, AgreementQueryVo queryVo) {
        IPage<AgreementVo> page = new Page<>(current, size);
        return leaseAgreementMapper.pageQuery(page, queryVo);
    }

    /**
     * 根据id查询租约信息
     * @param id
     * @return
     */
    public AgreementVo getAgreeById(Long id) {
        AgreementVo agreementVo = new AgreementVo();
        // 查询租约信息
        LeaseAgreement leaseAgreement = super.getById(id);
        BeanUtils.copyProperties(leaseAgreement, agreementVo);
        // 查询公寓信息
        Long apartmentId = leaseAgreement.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(apartmentId);
        // 房间信息
        Long room_id = leaseAgreement.getRoomId();
        RoomInfo roomInfo = roomInfoService.getById(room_id);

        //支付方式
        Long paymentTypeId = leaseAgreement.getPaymentTypeId();
        PaymentType paymentType = paymentTypeService.getById(paymentTypeId);

        //租期
        Long leaseTermId = leaseAgreement.getLeaseTermId();
        LeaseTerm leaseTerm = leaseTermService.getById(leaseTermId);


        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);
        return agreementVo;
    }
}




